﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace mdk.Cenas
{
    class KeyboardControllers
    {
        ContentManager content;
        Texture2D background;
        SpriteFont normalFont;
        SpriteFont focusFont;
        Dictionary<keyControllers, Command> controllers = new Dictionary<keyControllers, Command>();
        int currentOption;
        const int NUNBER_OF_COMMANDS = 4;
        enum keyControllers { FRENTE, BAIXO, ESQUERDA, DIREITA };

        public KeyboardControllers(ContentManager content)
        {
            this.content = content;
            background = content.Load<Texture2D>("img/black");
            normalFont = content.Load<SpriteFont>("font/normal");
            focusFont = content.Load<SpriteFont>("font/MenuFontSelected");

            controllers.Add(keyControllers.FRENTE, new Command("Movimento para Frente", "Up", 90, 70, true));
            controllers.Add(keyControllers.BAIXO, new Command("Movimento para Baixo", "Down", 90, 110, false));
            controllers.Add(keyControllers.ESQUERDA, new Command("Movimento para Esquerda", "Left", 90, 150, false));
            controllers.Add(keyControllers.DIREITA, new Command("Movimento para Direita", "Right", 90, 190, false));

        
            //controllers.Add("SALTAR", Keys.D);
            //controllers.Add("ATIRAR", Keys.S);
            //controllers.Add("SNIPER", Keys.A);
            currentOption = 0;

        }

        public void update(GameTime gameTime, KeyboardState currentKeyboard, KeyboardState lastKeyboard)
        {
            if (currentKeyboard.IsKeyDown(Keys.Tab) && (lastKeyboard != currentKeyboard))
            {
                currentOption++;
                currentOption = (currentOption < NUNBER_OF_COMMANDS) ? currentOption : 0;
                for ( int i = 0; i < NUNBER_OF_COMMANDS; i++ )
                {
                    if (i != currentOption)
                    {
                        controllers[(keyControllers)i].setFocus(false);
                    }
                    else {
                        controllers[(keyControllers)i].setFocus(true);
                    }
                }
            }
        }

        public void draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(background, new Rectangle(0, 0, 700, 525), Color.White);

            for ( int i = 0; i < NUNBER_OF_COMMANDS; i++ )
            {
                drawCommand(
                    spriteBatch,
                    controllers[ (keyControllers) i ]
                );
            }
            
            //spriteBatch.DrawString(font, "Movimento para Frente", new Vector2(90, 50), Color.White);
            //spriteBatch.DrawString(font, "Movimento para Traz", new Vector2(90, 70), Color.White);
            //spriteBatch.DrawString(font, "Movimento para Esquerda", new Vector2(90, 90), Color.White);
            //spriteBatch.DrawString(font, "Movimento para Direita", new Vector2(90, 110), Color.White);

            //spriteBatch.DrawString(font, "Saltar", new Vector2(10, 130), Color.White);
            //spriteBatch.DrawString(font, "Atirar", new Vector2(10, 150), Color.White);
            //spriteBatch.DrawString(font, "Modo Sniper", new Vector2(10, 170), Color.White);
        }

        private void drawLabel(SpriteBatch spriteBatch, String text, int xPosition, int yPosition, Boolean focus = false)
        {    
             spriteBatch.DrawString((focus ? focusFont : normalFont), text, new Vector2(xPosition, yPosition), Color.White);
        }

        private void drawCommand(SpriteBatch spriteBatch, Command command)
        {
            int labelYPosition = command.isFocus() ? command.getYPosition() - 10 : command.getYPosition();
            drawLabel(spriteBatch, command.getKey(), command.getXPosition() - 60, command.getYPosition());
            drawLabel(spriteBatch, command.getLabel(), command.getXPosition(), labelYPosition, command.isFocus());
        }

    }
}
